const {harvest, up, build, repair, arrange, defense} = require("./RoleRoom");
const {occupy, outHarvest, reserve, outTrans, guard, outRepair} = require("./RoleOut");
const {shield, attack, dismantle, bandits} = require("./RoleAttack");
const {
    generateGuard, generateHarvest, generateUp, generateBuilder, generateRepair, generateButler,
    generateOutHarvest, generateReserve
} = require("./Create");
const {findStore} = require("./Find");
const {MyRoomList} = require("./Param");
/**
 * 清除死亡Creep内存
 */
module.exports.clearCreepMemory = function () {
    for (let name in Memory.creeps) {
        if (!Game.creeps[name]) {
            // Creep 已死亡，删除其内存
            delete Memory.creeps[name];
        }
        if (Memory.creeps[name] === null || Memory.creeps[name] === undefined || Memory.creeps[name] === "" || Memory.creeps[name] === {}) {
            delete Memory.creeps[name];
        }
    }
}

//自动模式
global.auto = function attackAuto() {
    Memory.attackSign = "auto";
}
//等待模式
global.wait = function attackWait() {
    Memory.attackSign = "wait";
}
//撤退模式
global.back = function attackBack() {
    Memory.attackSign = "back";
}
//移动模式
global.move = function attackMove() {
    Memory.attackSign = "move";
}

//修复模式
global.renew = function attackRenew() {
    Memory.attackSign = "renew";
}

//回收模式
global.recycle = function attackRecycle() {
    Memory.attackSign = "recycle";
}

global.openDraw = function () {
    Memory.openDraw = true;
}

global.resetDefault = function () {
    Memory.default = true;
}

global.stop = function () {
    Memory.default = false;
}

//创建像素
module.exports.createPixel = () => {
    if (Game.cpu.bucket >= 10000) {
        log("生产一个像素点", "success");
        Game.cpu.generatePixel();
    }
}
//日志系统
let log = (content, level = "info") => {
    if (content !== undefined && content !== "" && content != null) {
        let levelColor;
        let levelTitle;
        switch (level) {
            case "success":
                levelColor = "background-color:#67c23a";
                levelTitle = "成功";
                break;
            case "primary":
                levelColor = "background-color:#409eff";
                levelTitle = "主要";
                break;
            case "warning":
                levelColor = "background-color:#e6a23c";
                levelTitle = "警告";
                break;
            case "danger":
                levelColor = "background-color:#f56c6c";
                levelTitle = "危险";
                break;
            default:
                levelColor = "background-color:#909399";
                levelTitle = "基础";
        }
        console.log(`<span style="${levelColor};color:white;border-radius: 2px;padding:1px 5px;font-size: 10px">${levelTitle}</span>` + "." + content);
    }
}
/**
 * 自动绑定行为
 */
module.exports.autoBehavior = function () {
    Object.values(Game.creeps).forEach(function (creep) {
        //遍历所有士兵,并赋予能力
        let tick = creep.ticksToLive;
        switch (creep.memory.role) {
            case "收集兵":
                wellDead(creep, tick);
                harvest(creep);
                if (tick === 59) {
                    generateHarvest(creep.room);
                }
                break;
            case "防御兵":
                wellDead(creep, tick);
                defense(creep);
                break;
            case "升级兵":
                wellDead(creep, tick);
                up(creep);
                if (tick === 59) {
                    generateUp(creep.room);
                }
                break;
            case "建造兵":
                wellDead(creep, tick);
                build(creep);
                if (tick === 59) {
                    generateBuilder(creep.room);
                }
                break;
            case "维修兵":
                wellDead(creep, tick);
                repair(creep);
                if (tick === 59) {
                    generateRepair(creep.room);
                }
                break;
            case "管家":
                wellDead(creep, tick);
                arrange(creep);
                if (tick === 59) {
                    generateButler(creep.room);
                }
                break;
            case "战士":
                attack(creep);
                break;
            case "占领兵":
                occupy(creep);
                break;
            case "盾牌兵":
                shield(creep);
                break;
            case "护卫":
                defense(creep);
                break;
            case "外矿收集兵":
                outHarvest(creep);
                if (tick === 59) {
                    generateOutHarvest(creep.room);
                }
                break;
            case "预订兵":
                reserve(creep);
                if (tick === 59) {
                    generateReserve(creep.room);
                }
                break;
            case "外部搬运兵":
                outTrans(creep);
                break;
            case "守护兵":
                guard(creep);
                if (tick < 60) {
                    generateGuard(creep.room);
                }
                if (tick === 120) {
                    generateGuard(creep.room);
                }
                break;
            case "外部维修兵":
                outRepair(creep);
                break;
            case "拆迁兵":
                dismantle(creep);
                break;
            case "土匪":
                bandits(creep);
                break;
            default:
                break;
        }
    })
}
module.exports.drawRoom = () => {
    if (Memory.openDraw) {
        for (let roomName of MyRoomList) {
            let room = Game.rooms[roomName];
            if (room.memory.mustSpawning) {
                room.visual.text("👾: 等级" + room.controller.level + " : 等待孵化中", 0, 1, {
                    color: '#e6a23c',
                    align: 'left'
                })
            } else {
                room.visual.text("👾: 等级" + room.controller.level + " : 空闲", 0, 1, {color: '#67c23a', align: 'left'})
            }
            let creepsLength = room.find(FIND_MY_CREEPS).length;
            room.visual.text("房间人数💂: " + creepsLength, 0, 2, {align: 'left'})
            let available = room.energyAvailable;
            let CapacityAvailable = room.energyCapacityAvailable;
            let availableRatio = available / CapacityAvailable;
            if (availableRatio > 0.75) {
                room.visual.text("可用能源🍪: " + available, 0, 3, {color: '#67c23a', align: 'left'})
            } else {
                room.visual.text("可用能源🍪: " + available, 0, 3, {color: '#e6a23c', align: 'left'})
            }
            room.visual.text("能源总量🏆: " + CapacityAvailable, 0, 4, {color: 'grey', align: 'left'})

            let allEnergy = 0;
            for (let store of findStore(room, 1)) {
                allEnergy += store.usedStore();
            }
            room.visual.text("可用资源🥫: " + (allEnergy / 1000).toFixed(2) + "K", 0, 5, {
                color: '#409eff',
                align: 'left'
            })
            if(room.memory.peace){
                room.visual.text("房间状态: " + "和平", 0, 6, {color: '#67c23a', align: 'left'})
            }else{
                room.visual.text("房间状态: " + "战争", 0, 6, {color: 'red', align: 'left'})
            }
        }
        if (Game.time % 100 === 0) {
            Memory.openDraw = false;
        }
    }
}

function wellDead(creep, tick) {
    if (tick < 20) {
        creep.say("🙉")
        if (creep.usedStore() > 10) {
            let transStore = creep.findBindClose(findStore(creep.room, -1));
            if (transStore) {
                creep.bestTrans(transStore);
                return;
            }
            creep.suicide();
            return;
        }
        creep.suicide();
    }
}

module.exports.changeMemory = (status, creep, list) => {
    if (status === 0||status===-3) {
        creep.spawning = true;
    }else{
        // console.log("出现其他异常删除元素")
        // //删除指定元素
        // for (let i = 0; i < list.length; i++) {
        //     if(creep.name===list[i].name){
        //         list.splice(i,1);
        //     }
        // }
    }
}